<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin\Topic;
use App\Models\Admin\Weibo;
use App\Models\Home\User;
use App\Models\RTresult;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
    //显示前台用户列表
    public function index(){

        $users = User::paginate(3);

        return view('admin.user')->with('users', $users);
    }
    //更改前台用户登录状态
    public function update(Request $request){
        //将话题的{开启禁用}写出来

        //调用状态值
        $rt_result = new RTresult();
        //判断是否  显示 或者 禁用
        $is_allowed = $request->is_allowed;

        //获取  话题表 的ID
        $user_id = $request->user_id;
        if ($is_allowed == 1) {
            $is_allowed = 0;
        } else {
            $is_allowed = 1;
        }

        //根据  点击所在的ID  改变是否显示或者禁用
        $res = User::find($user_id)->update(['is_allowed' => $is_allowed]);
        if ($res) {
            $rt_result->status = true;
            $rt_result->message = $is_allowed;
            return $rt_result->toJson();
        }
    }
    //前台用户详情的状态是否可见
    public function status(Request $request){
        //调用状态值
        $rt_result = new RTresult();
        //判断是否  显示 或者 禁用
        $is_show = $request->is_show;
//        dd($is_show);
        //获取  用户表 的ID
        $detail_id = $request->detail_id;

        if ($is_show == 1) {
            $is_show = 0;
        } else {
            $is_show = 1;
        }
        //根据  点击所在的ID  改变是否显示或者禁用
        $res = Weibo::find($detail_id)
            ->update(['is_show' => $is_show]);


        if ($res) {
            $rt_result->status = true;
            $rt_result->message = $is_show;
            return $rt_result->toJson();
        }
    }
    //搜索分页
    public function page(Request $request){
        //默认的搜索框为post传值
        if ($request->isMethod('post')) {
            //获取填写的内容
            $contents = $request->contents;
            //得到数据库中的数据
            $db = User::all();
            //定义一个数组，用来保存拼接的语句
            $arr = array();
            //拼接原生的sql语句，1为true`
            $where = 1;
            //判断输入的内容是否存在，用来搜索
            if (!empty($contents)) {
                //拼接原生的sql查询条件  使用模糊查询
                $where .= " and phone like \"%" . $contents . "%\"";
                //拼接多条件查询
                $arr ['phone'] = "phone like \"%" . $contents . "%\"";
            }
            //菜单的请求值
            $is_gai = $request->is_gai;

            //菜单的值不为空
            if ($is_gai==1||$is_gai==0) {

                // +   拼接  菜单 查询语句
                $where .= " and is_allowed =" . $is_gai;
                //拼接  菜单  查询条件
                $arr ['is_allowed'] = $is_gai;

            }
            $db =User::whereRaw($where);
            $users = $db->paginate(3);
            //appends把条件追加到URL里面
            $users = $users->appends($arr);
            return view('admin.user', ['users' => $users]);
        }
        /***get的传递方式用来分页查询点击下一页***/
        $arr = array();
        $where = 1;
        //get的接收方式
        if (!empty($_GET['phone'])) {

            $where .= " and " . $_GET['phone'];
            $arr ['phone'] = $_GET['phone'];
        }
        $is_gai = $request->is_gai;
        $allowed = $_GET['is_allowed'];
        if ($allowed== 1 || $allowed == 0) {

            $where .= " and is_allowed =" . $allowed;
            $arr ['is_allowed'] = $allowed;

        }
        //原生sql拼接语句
        $db =User::whereRaw($where);
        //分页
        $users = $db->paginate(3);
        //appends把条件追加到URL里面
        $users = $users->appends($arr);

        return view('admin/user', ['users' => $users]);
    }

    //前台用户详情
    public function show($id){
       //用户列表
        $users = User::join('user_infos', 'users.id', '=', 'user_infos.user_id')
            ->select( 'users.*','user_infos.email','users.phone','users.created_at','user_infos.nickname')
            ->where('users.id', '=', $id)
            ->get();

        //用户列表详情
        /*$details=Topic::join('weibos', 'topics.id', '=', 'weibos.topic_id')
            ->select('weibos.*', 'topics.topic')
            //取出id指定的一条或者几条数据
            ->where('weibos.topic_id', '=', $id)
            ->get();*/
        $details=Weibo::where('user_id',$id)->get();
        return view('admin.user_show')
            ->with('users', $users)
            ->with('details',$details);
    }
}
